#import "TreeXMLParserAppDelegate.h"
#import "TreeXMLParser/TreeXMLParser.h"

@implementation TreeXMLParserAppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [self creatingExample];
    [self creatingFromDictionaryExample];
    [self parsingExample];
    [self queryingExample];
    [self errorHandlingExample];
}

- (void)creatingExample
{
    NSLog(@"CREATING EXAMPLE:");
    XMLNode *root = [[XMLNode alloc] init];
    
    XMLInternalNode *one = [[XMLInternalNode alloc] initWithParent:root
                            key:@"one" attributes:nil];
    [root.children addObject:one];
    [one release];
    
    XMLInternalNode *two1 = [[XMLInternalNode alloc] initWithParent:one
                             key:@"two" attributes:nil];
    [one.children addObject:two1];
    [two1 release];
    
    XMLLeafNode *three = [[XMLLeafNode alloc] initWithParent:two1
                          value:@"three"];
    [two1.children addObject:three];
    [three release];    
    
    NSDictionary *two2Attrs = [NSDictionary
                               dictionaryWithObjects:
                               [NSArray arrayWithObject:@"attr1"]
                               forKeys:
                               [NSArray arrayWithObject:@"val1"]];
    XMLInternalNode *two2 = [[XMLInternalNode alloc] initWithParent:one
                             key:@"two" attributes:two2Attrs];
    [one.children addObject:two2];
    [two2 release];
        
    NSString *xml = [root generateXML];
    [root release];
    NSString *path = [NSHomeDirectory()
                      stringByAppendingPathComponent:
                      @"Documents/creating_example.xml"];
    if ([xml writeToFile:path atomically:YES encoding:NSUTF8StringEncoding
         error:NULL])
        NSLog(@"%@ saved", path);
    else
        NSLog(@"unable to save %@", path);
}

- (void)creatingFromDictionaryExample
{
    NSLog(@"CREATING FROM DICTIONARY EXAMPLE:");
    NSMutableDictionary *rootChildren = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *oneChildren = [[NSMutableDictionary alloc] init];
    [oneChildren setObject:@"three" forKey:@"two"];
    [rootChildren setObject:oneChildren forKey:@"one"];
    XMLNode *root = [[XMLNode alloc] init];
    [root addChildrenFromDictionary:rootChildren];
    
    NSString *xml = [root generateXML];
    [root release];
    NSString *path = [NSHomeDirectory()
                      stringByAppendingPathComponent:
                      @"Documents/creating_from_dictionary_example.xml"];
    if ([xml writeToFile:path atomically:YES encoding:NSUTF8StringEncoding
         error:NULL])
        NSLog(@"%@ saved", path);
    else
        NSLog(@"unable to save %@", path);
}

- (void)parsingExample
{    
    NSLog(@"PARSING EXAMPLE:");
    XMLParser *parser = [[XMLParser alloc] init];
    NSString *path = [NSHomeDirectory()
                      stringByAppendingPathComponent:
                      @"Documents/creating_example.xml"];
    NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
    XMLNode *root = [parser parse:[file readDataToEndOfFile] error:nil];
    [parser release];
    NSLog(@"%@ parsed", path);
    
    NSString *xml = [root generateXML];
    path = [NSHomeDirectory()
            stringByAppendingPathComponent:
            @"Documents/creating_example_recreated.xml"];
    if ([xml writeToFile:path atomically:YES encoding:NSUTF8StringEncoding
                   error:NULL])
        NSLog(@"%@ saved", path);
    else
        NSLog(@"unable to save %@", path);
}

- (void)queryingExample
{
    NSLog(@"QUERYING EXAMPLE:");
    XMLParser *parser = [[XMLParser alloc] init];
    NSString *path = [NSHomeDirectory()
                      stringByAppendingPathComponent:
                      @"Documents/creating_example.xml"];
    NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
    XMLNode *root = [parser parse:[file readDataToEndOfFile] error:nil];
    [parser release];
    NSLog(@"%@ parsed", path);
    
    XMLNode *three = [root queryNode:@"one/two/"];
    NSLog(@"%@ found", [(XMLLeafNode *)three value]);

    NSArray *twos = [root queryNodes:@"one/two"];
    NSLog(@"%d twos found", [twos count]);
}

- (void)errorHandlingExample
{
    NSLog(@"ERROR HANDLING EXAMPLE:");
    XMLParser *parser = [[XMLParser alloc] init];
    NSData *xml = [@"<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
                   <one><two1><three></two1><two2></one>"
                   dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    XMLNode *root = [parser parse:xml error:&err];
    if (err == nil)
        NSLog(@"Parsed without errors");
    else
        NSLog(@"Parsing error: %@", [err localizedDescription]);
    
    NSString *parsedXml = [root generateXML];
    NSString *path = [NSHomeDirectory()
                      stringByAppendingPathComponent:
                      @"Documents/error_handling_example.xml"];
    if ([parsedXml writeToFile:path atomically:YES encoding:NSUTF8StringEncoding
         error:NULL])
        NSLog(@"%@ saved", path);
    else
        NSLog(@"unable to save %@", path);
}

@end
